//
//  LanageTableViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/8.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "LanageTableViewController.h"
#import "LanguageCell.h"
@interface LanageTableViewController ()

@end

@implementation LanageTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
    
}
- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LanguageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LanguageCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[LanguageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LanguageCell"];
    }
    if (indexPath.row==0) {
        cell.languageLabel.text=@"English";
    }else{
        cell.languageLabel.font=[UIFont systemFontOfSize:17];
        cell.languageLabel.text=@"简体中文";
    }
    cell.rightImage.hidden=YES;
    if ([[UserDefaults objectForKey:@"appLanguage"] isEqualToString:@"zh-Hans"]&&[cell.languageLabel.text isEqualToString: @"简体中文"]) {
        cell.rightImage.hidden=NO;
    }else if ([[UserDefaults objectForKey:@"appLanguage"] isEqualToString:@"en"]&&[cell.languageLabel.text isEqualToString: @"English"]){
        cell.rightImage.hidden=NO;
    }
    
    
    return cell;
    
    
    
    
    
}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

{
    LanguageCell *cell = (LanguageCell*)[tableView cellForRowAtIndexPath:indexPath];
    NSString*language;
    if ([cell.languageLabel.text isEqualToString: @"English"]) {
        language=@"2";
    }else {
        language=@"1";
    }
    if ([UserDefaults objectForKey:K_LOGININFO_TOKEN]) {
        NSDictionary *params = @{@"lang":language};
        [LSRequestManager putRequestWithURL:HUOBI(@"api/user/favSet")
                                      params:params
                                     success:^(NSDictionary *responseData) {
                                         if ([responseData[@"ret"] integerValue ] == 0) {
                                             //修改本地获取的语言文件-交替
                                             if ([cell.languageLabel.text isEqualToString: @"English"]) {
                                                 [UserDefaults setObject:@"en" forKey:@"appLanguage"];
                                             }else {
                                                 [UserDefaults setObject:@"zh-Hans" forKey:@"appLanguage"];
                                             }
                                             [UserDefaults synchronize];
                                             NSLog(@"%@",Localized(@"hello"));
                                             [self.tableView reloadData];
                                         } else {
                                             [MBProgressHUD showError:responseData[@"msg"]];
                                         }
                                     }
                                     failure:^(NSError *error) {
                                         
                                     }
                                     showHUD:NO];
    }else{
        if ([cell.languageLabel.text isEqualToString: @"English"]) {
            [UserDefaults setObject:@"en" forKey:@"appLanguage"];
        }else {
            [UserDefaults setObject:@"zh-Hans" forKey:@"appLanguage"];
        }
        [UserDefaults synchronize];
        
        [self.tableView reloadData];
    }
    
    
    
    
    
    
    
    
    
//    LanguageCell *cell = (LanguageCell*)[tableView cellForRowAtIndexPath:indexPath];
//    cell.rightImage.hidden=NO;
    
    
}
//- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath
//
//{
//
//    LanguageCell *cell = (LanguageCell*)[tableView cellForRowAtIndexPath:indexPath];
//    cell.rightImage.hidden=YES;
//
//}




@end
